package com.yanqu.road.server.cmd.cross.player;

import com.yanqu.road.entity.enums.*;
import com.yanqu.road.entity.interactive.InteractiveRecord;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.logic.bussiness.player.UserAttributeBussiness;
import com.yanqu.road.logic.bussiness.player.UserBussiness;
import com.yanqu.road.logic.bussiness.player.UserInteractiveBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.cmd.base.AbstractServerCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.player.EarnSpeedModule;
import com.yanqu.road.server.gameplayer.module.player.InteractiveModule;
import com.yanqu.road.server.manger.config.InteractiveMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.rank.RankMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.math.BigInteger;

@Cmd(code = Protocol.S_CROSS_HATE_PLAYER, desc = "跨服蛊惑玩家")
public class CrossHateCmd extends AbstractServerCommand {

	private long userId;
	private UserBaseInfo userBaseInfo;
	private long hateUserId;

	private int heartCount;
	private UserInfo hateUserInfo;
	private BigInteger effectValue = BigInteger.ZERO;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		PlayerProto.CrossHatePlayerReqMsg reqMsg = PlayerProto.CrossHatePlayerReqMsg.parseFrom(packet.getBytes());
		userId = reqMsg.getUserId();
		userBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getPlayerBaseData());
		hateUserId = reqMsg.getHateUserId();
		return true;
	}

	@Override
	public void execute(YanQuMessage packet) throws Exception {
		hateUserInfo = UserMgr.getUserInfo(hateUserId);
		if(null == hateUserInfo){
			ret = GameErrorCode.E_USER_NO_FOUND;
			return;
		}
		GamePlayer hatePlayer = GamePlayerMgr.getOnlinePlayer(hateUserId);
		synchronized (hateUserInfo){
			if ((hateUserInfo.getTitleId() - userBaseInfo.getTitleId()) >= GameConfig.TRICK_LEVEL_LOCK) {
				ret = GameErrorCode.E_HATE_TITLE_NO_ENOUGH;
				return;
			}
			if(hateUserInfo.getTodayBeHateTimes() >= GameConfig.ONE_DAY_PLAYER_BE_HATE_MAX_TIMES){
				ret = GameErrorCode.E_PLAYER_BE_HATE_MAX_TIMES;
				return;
			}
			int limitValue = InteractiveMgr.getBeHateLimit();
			heartCount = hateUserInfo.getHeartCount();
			if(heartCount <= limitValue){
				ret = GameErrorCode.E_PLAYER_CURRENT_HATE_LIMIT;
				return;
			}
			heartCount--;
			hateUserInfo.setHeartCount(heartCount);
			hateUserInfo.setTodayBeHateTimes(hateUserInfo.getTodayBeHateTimes() + 1);
			effectValue = UserMgr.getHeartEffectEarnSpeed(hateUserInfo.getBaseEarnSpeed());
			if(null != hatePlayer){
				hatePlayer.getModule(AttributeModule.class).syncAttribute(ePlayerAttrType.HeartCount);
				//hatePlayer.getModule(EarnSpeedModule.class).scheduleAddSilver();
			}else {
				hateUserInfo.setEarnSpeed(hateUserInfo.getEarnSpeed().subtract(effectValue));
				//赚速榜
				RankMgr.changeUserRank(eBigRankType.Normal.getValue(), hateUserInfo.getUserId(), hateUserInfo.getEarnSpeed(), eRankType.EarnSpeed.getValue(), "");
			}
		}
		if(null != hatePlayer){
			//hatePlayer.getModule(AttributeModule.class).syncAttribute(ePlayerAttrType.HeartCount);
			hatePlayer.getModule(EarnSpeedModule.class).scheduleAddSilver();
		}
		if(-1 == heartCount){
			hateUserInfo.setLastHeartRecoverTime(System.currentTimeMillis() / 1000);
			if (null == hatePlayer) {
				UserBussiness.updateLastHeartRecoverTimeAndEarnSpeedAndHateTimes(hateUserId, heartCount, hateUserInfo.getLastHeartRecoverTime(), hateUserInfo.getEarnSpeed(),
						hateUserInfo.getTodayBeHateTimes());
			}
		}else {
			if (null == hatePlayer) {
				UserBussiness.updateHeartCountAndEarnSpeedAndHateTimes(hateUserId, heartCount, hateUserInfo.getEarnSpeed(), hateUserInfo.getTodayBeHateTimes());
			}
		}
		if(null == hatePlayer){
			UserAttributeBussiness.updateAddUserAttribute(hateUserId, ePlayerAttrType.TotalBeHateTimes, 1);
		}else {
			AttributeModule hateAttributeModule = hatePlayer.getModule(AttributeModule.class);
			hateAttributeModule.addAttribute(ePlayerAttrType.TotalBeHateTimes, 1);
			hatePlayer.notifyListener(eGamePlayerEventType.TotalBeHateTimes.getValue(), hateAttributeModule.getAttribute(ePlayerAttrType.TotalBeHateTimes));
		}
		InteractiveRecord interactiveRecord = UserMgr.initInteractiveRecord(hateUserId, eInteractiveType.hate.getValue(), userId, userBaseInfo);
		if(null != hatePlayer) {
			hatePlayer.getModule(InteractiveModule.class).addInteractiveRecord(interactiveRecord, effectValue);
		}else {
			UserInteractiveBussiness.addInteractiveRecord(interactiveRecord);
		}
	}

	@Override
	public void sendData(YanQuMessage packet) throws Exception {
		PlayerProto.CrossHatePlayerRespMsg.Builder respMsg = PlayerProto.CrossHatePlayerRespMsg.newBuilder();
		respMsg.setUserId(userId);
		respMsg.setServerId(userBaseInfo.getServerId());
		respMsg.setHateUserId(hateUserId);
		respMsg.setRet(ret);
		if(0 == ret){
			respMsg.setHeartCount(heartCount);
			respMsg.setEffectValue(effectValue.toString());
			respMsg.setEarnSpeed(hateUserInfo.getEarnSpeed().toString());
		}
		YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_HATE_PLAYER_RESP, respMsg);
		GamePlayerMgr.sendPacket(0, pbMsg);
	}
}
